var PostAritcle = {
    strip_tags:function(input, allowed) {
        allowed = (((allowed || "") + "")
            .toLowerCase()
            .match(/<[a-z][a-z0-9]*>/g) || [])
        .join('');
        var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
        commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
        return input.replace(commentsAndPhpTags, '').replace(tags, function($0, $1){
            return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
        });
    },
    initFormPost:function() {
        if ( $("#ck_content")[0] != undefined ) {
            if ( typeof(isPostOnAdmin)!='undefined' && isPostOnAdmin==1 ) {
                addCkEditorAdmin('ck_content');
            } else {
                addCkEditor('ck_content');
            }
        }
        
        $("#frmPost").submit(function() {
            var error       = 0;
            var $title      = $("#txtTitle");
            var $titleErr   = $("#title_err");
            var title       = $.trim(PostAritcle.strip_tags($title.val()));
            $title.val(title);

            /**
             * Check title
             */
            if ( title.length <= 0 ) {
                $titleErr.html('Vui lòng nhập tiêu đề.<br>').show();
                error   = 1;
            } else {
                $titleErr.text('').hide();
            }

            /**
             * Check content
             */
            var content     = CKEDITOR.instances["ck_content"].getData();
            var $contentErr = $("#content_err");
            if ( content.length <= 0 ) {
                $contentErr.html('Vui lòng nhập nội dung.<br>').show();
                error   = 1;
            } else {
                $contentErr.text('').hide();
            }

            return !error;
        });
    },
    post:function() {
        $("#frmPost").attr('action', domain_url+'/post.html');
    },
    preview:function() {
        $("#frmPost").attr('action', domain_url+'/preview.html');
    }
}

$(function(){
    PostAritcle.initFormPost();
});